/**
 * @file MessengerClient.cpp
 * @author chenlei (1475076156@qq.com)
 * @brief The client then reads the IOR string from the file and converts it back into an
          object reference.
 * @version 0.1
 * @date 2022-08-08
 *
 * @copyright Copyright (c) 2022 北京航天拓扑高科技有限责任公司
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2022-08-08 <td>v1.0     <td>chen     <td>内容
 * </table>
 */
#include "MessengerC.h"
#include <iostream>
int main(int argc, char* argv[]) {
    try {
        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

        CORBA::Object_var obj = orb->string_to_object("file://Messenger.ior");  // 解析 Messenger object's IOR 对象标识.
        if (CORBA::is_nil(obj.in())) {
            std::cerr << "Could not get Messenger IOR." << std::endl;
            return -1;
        }

        Messenger_var messenger =
            Messenger::_narrow(obj.in());  // Narrow the IOR to a Messenger object reference对象引用.
        if (CORBA::is_nil(messenger.in())) {
            std::cerr << "IOR was not a Messenger object reference." << std::endl;
            return -1;
        }

        // 业务处理
        for (int i = 0; i < 10; i++) {
            Sleep(1000);
            // Send a message the the Messenger object.
            CORBA::String_var msg = CORBA::string_dup("Hello!");
            messenger->send_message("TAO User", "Test", msg.inout());
            // Print the Messenger's reply.
            std::cout << "Reply: " << msg.in() << std::endl;
        }
        return 0;
    } catch (CORBA::Exception& ex) {
        std::cerr << "MessengerClient CORBA exception: " << ex << std::endl;
    }
    return -1;
}